<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
      <template #default="{ filterForm, handleQuery }">
        <el-col :span="10">
          <el-form-item label="年度细则">
            <el-date-picker
              type="year"
              @change="changeQueryYear"
              v-model="filterForm.yearD"
              @keyup.enter="handleQuery"
              :disabled-date="disabledDate"
              value-format="YYYY"
              style="width: 100px"
            />
            <el-select
              @change="handleQuery"
              v-model="filterForm.fineGauge"
              :disabled="!filterForm.yearD"
              placeholder=""
              style="width: 300px"
            >
              <el-option
                v-for="ele in queryRules"
                :key="ele"
                :value="ele"
                :label="ele"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </template>
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['sysHiddenCheck:create']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
    >
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['sysHiddenCheck:update']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['sysHiddenCheck:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['nianduxize']"
    >
      <template #nianduxize>
        <el-form-item label="年度细则">
          <el-date-picker
            type="year"
            @change="changeAddYear"
            v-model="form.yearD"
            :disabled-date="disabledDate"
            value-format="YYYY"
            style="width: 100px"
          />
          <el-select
            v-model="form.fineGauge"
            :disabled="!form.yearD"
            placeholder=""
            style="width: 300px"
          >
            <el-option
              v-for="ele in addRules"
              :key="ele"
              :value="ele"
              :label="ele"
            ></el-option>
          </el-select>
        </el-form-item> </template
    ></DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/hdanger/jcdd.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
const { proxy } = getCurrentInstance();
const disabledDate = (time) => {
  //Date.now()是javascript中的内置函数，它返回自1970年1月1日00:00:00 UTC以来经过的毫秒数。
  return time.getTime() > Date.now();
};

const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    [
      "checkUnit",
      "dangerName",
      "checkType",
      "hiddenLevel",
      "hiddenType",
    ].includes(i.prop)
  ),
});
const showSearch = ref(true);

const dialogVform = ref();

const table = ref();

const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  form,
} = useAsyncData({ APIS, formJson, dialogVform, table, proxy });
// 生成年度细则
const queryRules = ref([]);
const addRules = ref([]);
const createYearRules = (year) => {
  const nowTime = new Date();
  const nowYear = nowTime.getFullYear();
  const nowMonth = nowTime.getMonth() + 1;
  if (year < nowYear || nowMonth >= 7) {
    return [
      `油气储存企业安全风险评估细则（${year}年第一次）`,
      `油气储存企业安全风险评估细则（${year}年第二次）`,
    ];
  } else {
    return [`油气储存企业安全风险评估细则（${year}年第一次）`];
  }
};
const changeQueryYear = (year) => {
  queryRules.value = createYearRules(year);
};
const changeAddYear = (year) => {
  addRules.value = createYearRules(year);
};
</script>

<style lang="scss" scoped></style>
